package org.topteam.ui.easyui;

import javax.servlet.jsp.JspWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 枫 on 2014/8/6.
 */
public class BodyTag extends TagRender {
    private boolean full = true;
    private String var;

    @Override
    public String getEasyuiTag() {
        return "layout";
    }

    @Override
    public int renderBody() {
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public Map<String, Object> getOptions() {
        Map<String,Object> options = new HashMap<String, Object>();
        options.put("full",full);
        return options;
    }

    @Override
    public void renderStart(JspWriter out) {
        try {
            out.write("<body");
            if (full) {
                out.write(" class=\"easyui-"+getEasyuiTag()+"\"");
            }
            out.write(">");
            if (full) {
                out.write("<div data-options=\"region:'center',border:false\">");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void renderEnd(JspWriter out) {
        try {
            if (full) {
                out.write("</div>");
            }
            if(getVar() == null){
                setVar("body"+UUID.randomUUID().toString().replace("-",""));
            }
            getRenderContext().write(out,getVar());
            out.write("</body>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isFull() {
        return full;
    }

    public void setFull(boolean full) {
        this.full = full;
    }

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }
}
